﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using Hjp.FrameWork.Common;
using Wjtour.BLL;

namespace Wjtour.Ajax
{
    /// <summary>
    /// Summary description for login
    /// </summary>
    public class memberregesiter : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            var mobile = context.Request["mobile"];
            var email = context.Request["email"];
            var code = context.Request["code"];
            var pwd = context.Request["pwd"];
            var confirmpwd = context.Request["confirmpwd"];
            var sure = context.Request["sure"];

            Result r = new Result();
            if (string.IsNullOrWhiteSpace(mobile))
            {
                r.Errcode = "1";
                r.Errmsg = "请输入正确的手机号码！";
            }
            else if (string.IsNullOrWhiteSpace(email))
            {
                r.Errcode = "2";
                r.Errmsg = "请输入正确的邮箱地址！";
            }
            else if (string.IsNullOrWhiteSpace(pwd))
            {
                r.Errcode = "3";
                r.Errmsg = "请输入正确的密码！";
            }
            else if (string.IsNullOrWhiteSpace(confirmpwd))
            {
                r.Errcode = "4";
                r.Errmsg = "请输入确认密码！";
            }
            else if (confirmpwd != pwd)
            {
                r.Errcode = "5";
                r.Errmsg = "两次密码输入不一致！";
            }
            else if (string.IsNullOrWhiteSpace(code))
            {
                r.Errcode = "6";
                r.Errmsg = "请输入正确的验证码！";
            }
            else if (code.ToUpper() != context.Session["CheckCode"].ToString())
            {
                r.Errcode = "7";
                r.Errmsg = "验证码输入不正确！";
            }
            else if (sure.ToUpper() != "TRUE")
            {
                r.Errcode = "8";
                r.Errmsg = "您必须同意注册协议！";
            }
            else
            {
                Wjtour.BLL.member_bll m_bll = new member_bll();
                Wjtour.Domain.member member = m_bll.CheckEmail(email.Trim());
                if (member == null)
                {
                    member = new Domain.member();
                    member.mobile = mobile;
                    member.email = email;
                    member.pwd = pwd;
                    member.regtime = DateTime.Now;
                    member.regip = Utils.GetRealIP();

                    int id = 0;
                    bool b = m_bll.Save(member, out id);
                    if (b)
                    {
                        member.id = id;
                        r.Errcode = "0";
                        r.Errmsg = "注册成功！";
                        context.Session["CurrentMember"] = member;
                    }
                    else
                    {
                        r.Errcode = "12";
                        r.Errmsg = "注册失败！";
                    }
                }
                else
                {
                    r.Errcode = "11";
                    r.Errmsg = "该邮箱已被注册过了！";
                }
            }

            JavaScriptSerializer serialzier = new JavaScriptSerializer();
            string output = serialzier.Serialize(r);
            context.Response.Write(output);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    [Serializable]
    public class Result
    {
        public string Errcode { get; set; }
        public string Errmsg { get; set; }
        public object Data { get; set; }
    }
}